from django.db import models

from lapi_app.models.environment_model.environment import Environment
from lapi_app.models.user_model.user_info import Userinfo


class Testcase(models.Model):
    t_name = models.CharField("用例名称", null=True, max_length=50)
    t_method = models.IntegerField("请求方法", null=True)  # 1：get， 2：post， 3：put， 4：delete
    t_url = models.TextField("url请求地址", null=True)
    t_params = models.JSONField("请求参数", null=True, default=dict, max_length=255)
    t_type = models.IntegerField("参数类型", null=True, default=2)  # 1:form-data， 2:Json， 3:x-www-form-urlencoded， 4:None
    api_type = models.IntegerField("接口类型", null=True, default=1)  # 0：普通接口  1：海外接口  2：国内接口
    t_header = models.JSONField("请求头", null=True, default=dict)
    t_case_detail = models.TextField("接口说明", null=True, default="")
    t_body = models.JSONField("请求体", null=True, default=dict)
    t_extract = models.JSONField("参数提取", null=True, default=list, max_length=255)
    update_time = models.DateTimeField("更新时间", auto_now=True)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    t_environment = models.ForeignKey(Environment, on_delete=models.CASCADE, default=1)
    t_user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)

    def __str__(self):
        return self.t_name


# 本地变量
class Variable_local(models.Model):
    local_name = models.CharField("变量名称", null=False, max_length=255)
    local_data = models.CharField("变量路径", null=True, max_length=255)
    local_result = models.TextField("变量结果", null=False)
    local_description = models.CharField("变量描述", null=True, max_length=255)
    update_time = models.DateTimeField("更新时间", auto_now=True)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    local_user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)

    def __str__(self):
        return self.local_name


# 断言
class Asserts(models.Model):
    assert_key = models.CharField("断言字段名", null=False, max_length=255)
    assert_value = models.TextField("断言结果", null=False)
    assert_status = models.CharField("断言状态", null=True, max_length=255)
    assert_testcase = models.ForeignKey(Testcase, on_delete=models.CASCADE)

    def __str__(self):
        return self.assert_key


# 参数化
class Parametric(models.Model):
    file_name = models.CharField("文件名", null=False, max_length=255)
    parametric_type = models.IntegerField("参数化类型", null=True)  # 1：params  2： Body  3：Assert
    file_content = models.TextField("内容体", null=True)
    update_time = models.DateTimeField("更新时间", auto_now=True)
    p_testcase = models.ForeignKey(Testcase, on_delete=models.CASCADE, null=True)
    p_user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)

    def __str__(self):
        return self.file_name


# 参数化结果
class Parametric_result(models.Model):
    url = models.CharField("请求地址", null=False, max_length=255)
    method = models.IntegerField("请求方法", null=True)
    params = models.CharField("请求参数", null=True, default=[], max_length=255)
    body = models.TextField("请求体", null=True, default={})
    assert_data = models.TextField("断言", null=True, default=[])
    status = models.IntegerField("断言状态", null=True, default=1)
    response = models.TextField("请求结果", null=True, default={})
    response_time = models.CharField("响应时间", null=True, max_length=255)
    p_user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)
    p_testcase = models.ForeignKey(Testcase, on_delete=models.CASCADE, null=True)


# 接口处理结果
class Batch_list_result(models.Model):
    url = models.CharField("请求地址", null=False, max_length=255)
    method = models.IntegerField("请求方法", null=True)
    params = models.CharField("请求参数", null=True, default=[], max_length=255)
    body = models.TextField("请求体", null=True, default={})
    assert_data = models.TextField("断言", null=True, default=[])
    status = models.IntegerField("断言状态", null=True, default="true")
    response = models.TextField("请求结果", null=True, default={})
    response_time = models.CharField("响应时间", null=True, max_length=255)
    p_user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)
    p_testcase = models.ForeignKey(Testcase, on_delete=models.CASCADE, null=True)
    extract_result = models.TextField("参数提取结果", null=True, default=[])


class case_result(models.Model):
    url = models.CharField("请求地址", null=False, max_length=255)
    method = models.IntegerField("请求方法", null=True)
    params = models.JSONField("请求参数", null=True, default=dict, max_length=255)
    body = models.JSONField("请求体", null=True, default=dict)
    header = models.JSONField("请求头", null=True, default=dict)
    assert_data = models.JSONField("断言", null=True, default=list)
    extract = models.JSONField("参数提取", null=True, default=list, max_length=255)
    response = models.JSONField("请求结果", null=True, default=dict)
    status = models.IntegerField("执行状态", null=True, default="true")
    case = models.ForeignKey(Testcase, on_delete=models.CASCADE, null=True)
    user = models.ForeignKey(Userinfo, on_delete=models.CASCADE)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    response_time = models.CharField("响应时间", null=True, max_length=255)

    def __str__(self):
        return self.url

